﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Core.Model;
using Core.Service;
using Core.Repository;

namespace Service
{
    public class Service<TObj> : IService<TObj, Guid> where TObj : EntityBase<Guid>, new()
    {
        protected IRepository<Guid, TObj> Repo;

        public Service(IRepository<Guid, TObj> repo)
        {
            this.Repo = repo;
        }

        public Guid Create(TObj o)
        {
            o.DtCreation = DateTime.Now;
            Repo.Insert(o);
            Repo.Save();
            return o.Id;
        }

        public void Save()
        {
            Repo.Save();
        }

        public void Delete(Guid id)
        {
            Repo.Delete(Repo.Get(id));
        }

        public TObj Get(Guid id)
        {
            return Repo.Get(id);
        }

        public IQueryable<TObj> GetAll()
        {
            return Repo.GetAll();
        }

        public IQueryable<TObj> Where(Expression<Func<TObj, bool>> func)
        {
            return Repo.Where(func);
        }
    }
}
